<?php
namespace app\api\model;


use app\model\Base;

class Merchant extends Base
{
    public static function induce($merchantId, $amount, $alias = 'goods_money', $type = 1)
    {
        /**
         * TODO
         * $type 判断是否需要手续费
         * 手续费比例后台配置，直接获取后台手续费比例
         * 加手续费字段，扣除手续费逻辑
         */
        $merchant = self::find($merchantId);
        $add = [];
        $add['merchant_id'] = $merchantId;
        $add['amount'] = $amount;
        $add['type'] = $type;
        $add['before'] = $merchant[$alias];

        $update = [];
        $update[$alias] = $add['before'] + $amount;
        $merchant->save($update);

        switch ($alias)
        {
            case 'turnover':{
                $add['type'] = Base::$walletTypeArray[$type];
                $add['content'] = Base::$walletLogDesArray[$type];
                MerchantTurnoverLog::create($add);
            }
                break;
            case 'bonus_points':{
                $add['type'] = Base::$walletTypeArray[$type];
                $add['content'] = Base::$walletLogDesArray[$type];
                MerchantBonusPointsLog::create($add);
            }
                break;
            case 'bonus_cash':{
                $add['type'] = Base::$walletTypeArray[$type];
                $add['content'] = Base::$walletLogDesArray[$type];
                MerchantBonusCashLog::create($add);
            }
                break;
            case 'shareholder_gold':
            case 'company_gold':{
                MerchantLog::create($add);
            }
                break;
        }
        return true;
    }

    public static function deduce($merchantId, $amount, $alias = 'goods_money', $type = 1)
    {
        /**
         * TODO
         * $type 判断是否需要手续费
         * 手续费比例后台配置，直接获取后台手续费比例
         * 加手续费字段，扣除手续费逻辑
         */
        $merchant = self::find($merchantId);
        $add = [];
        $add['merchant_id'] = $merchantId;
        $add['amount'] = -$amount;
        $add['type'] = $type;
        $add['before'] = $merchant[$alias];

        $update = [];
        $update[$alias] = $add['before'] - $amount;
        $merchant->save($update);

        switch ($alias)
        {
            case 'turnover':{
                $add['type'] = Base::$walletTypeArray[$type];
                $add['content'] = Base::$walletLogDesArray[$type];
                MerchantTurnoverLog::create($add);
            }
                break;
            case 'bonus_points':{
                $add['type'] = Base::$walletTypeArray[$type];
                $add['content'] = Base::$walletLogDesArray[$type];
                MerchantBonusPointsLog::create($add);
            }
                break;
            case 'bonus_cash':{
                $add['type'] = Base::$walletTypeArray[$type];
                $add['content'] = Base::$walletLogDesArray[$type];
                MerchantBonusCashLog::create($add);
            }
                break;
            case 'shareholder_gold':
            case 'company_gold':{
                MerchantLog::create($add);
            }
                break;
        }
        return true;
    }
}